﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Dapper;
using PMS.IRepository;
using PMS.Models.Base;
using PMS.Utils;

namespace PMS.Repository
{
    public class BaseLogRepository : BaseRepository<BaseLog>, IBaseLogRepository
    {
        #region 公共方法


        public int Add(BaseLog user)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into BaseLog(");
            strSql.Append("Id,Title,Type,Content,DealWith,DealWithDate,IsDel,CreateBy,CreateDate,ModifyBy,ModifyDate,DeleteBy,DeleteDate)");
            strSql.Append(" values (");
            strSql.Append("@Id,@Title,@Type,@Content,@DealWith,@DealWithDate,@IsDel,@CreateBy,@CreateDate,@ModifyBy,@ModifyDate,@DeleteBy,@DeleteDate)");
            strSql.Append(";select @@IDENTITY");
            using (DbConn)
            {
                return DbConn.Execute(strSql.ToString(), user);
            }
        }

        public int Edit(BaseLog user)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update BaseLog set ");
            strSql.Append("Title=@Title,");
            strSql.Append("Type=@Type,");
            strSql.Append("Content=@Content,");
            strSql.Append("DealWith=@DealWith,");
            strSql.Append("DealWithDate=@DealWithDate,");
            strSql.Append("IsDel=@IsDel,");
            strSql.Append("CreateBy=@CreateBy,");
            strSql.Append("CreateDate=@CreateDate,");
            strSql.Append("ModifyBy=@ModifyBy,");
            strSql.Append("ModifyDate=@ModifyDate,");
            strSql.Append("DeleteBy=@DeleteBy,");
            strSql.Append("DeleteDate=@DeleteDate");
            strSql.Append(" where Id=@Id ");
            using (DbConn)
            {
                return DbConn.Execute(strSql.ToString(), user);
            }
        }

        public BaseLog Dtl(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,Title,Type,Content,DealWith,DealWithDate,IsDel,CreateBy,CreateDate,ModifyBy,ModifyDate,DeleteBy,DeleteDate from BaseLog ");
            strSql.Append(" where Id=@Id ");
            using (DbConn)
            {
                return DbConn.QueryFirstOrDefault<BaseLog>(strSql.ToString(), new { Id = id });
            }
        }

        public bool IsExists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from BaseLog");
            strSql.Append(" where Id=@Id");
            using (DbConn)
            {
                return DbConn.ExecuteScalar(strSql.ToString(), new { Id = id }).ToInt32() > 0;
            }
        }


        public int Delete(int id, string deleteBy)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("Update BaseLog  Set ");
            strSql.Append(" IsDel=1, ");
            strSql.Append(" DeleteBy=@DeleteBy, ");
            strSql.Append(" DeleteDate=GetDate() ");
            strSql.Append(" where Id=@Id");
            using (DbConn)
            {
                return DbConn.Execute(strSql.ToString(), new { Id = id, DeleteBy = deleteBy });
            }
        }

        public int Delete(int[] ids, string deleteBy)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("Update BaseLog  Set ");
            strSql.Append(" IsDel=1, ");
            strSql.Append(" DeleteBy=@DeleteBy, ");
            strSql.Append(" DeleteDate=GetDate() ");
            strSql.Append(" where Id in @Ids");
            using (DbConn)
            {
                return DbConn.Execute(strSql.ToString(), new { Id = ids.ToArray(), DeleteBy = deleteBy });
            }
        }

        public List<BaseLog> GetList()
        {
            string strSql = "Select * from BaseLog";
            using (DbConn)
            {
                return DbConn.Query<BaseLog>(strSql).ToList();
            }
        }
        public List<BaseLog> GetList(Hashtable ht)
        {
            StringBuilder sb = new StringBuilder();
            Dictionary<string, object> param = new Dictionary<string, object>();
            GetStrWhereAndParam(ht, sb, param);
            string strSql = "Select * from BaseLog where 1=1 " + sb;
            using (DbConn)
            {
                return DbConn.Query<BaseLog>(strSql, param).ToList();
            }
        }

        public int GetCount(Hashtable ht)
        {
            StringBuilder sb = new StringBuilder();
            Dictionary<string, object> param = new Dictionary<string, object>();
            GetStrWhereAndParam(ht, sb, param);
            string strSql = "Select * from BaseLog where 1=1 " + sb;
            using (DbConn)
            {
                return DbConn.ExecuteScalar(strSql, param).ToInt32();
            }
        }


        public void GetStrWhereAndParam(Hashtable ht, StringBuilder sb, Dictionary<string, object> param)
        {
            if (ht.IsNullOrEmptyByKey("IsDel"))
            {
                sb.Append(" And IsDel=@IsDel");
                param.Add("IsDel", ht["IsDel"]);
            }
            else
            {
                sb.Append(" And IsDel=0");
            }

            if (ht.IsNullOrEmptyByKey("Title"))
            {
                sb.Append(" And Title Like @Title");
                param.Add("Title", "%" + ht["Title"] + "%");
            }

            if (ht.IsNullOrEmptyByKey("DealWith"))
            {
                sb.Append(" And DealWith=@DealWith");
                param.Add("DealWith", ht["DealWith"]);
            }

        }
        #endregion

    }
}